package org.groupg.practice.jidesoft;


import com.jidesoft.swing.CheckBoxList;
import com.jidesoft.swing.CheckBoxListSelectionModel;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

public class CheckBoxListExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("复选框列表示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setLayout(new BorderLayout());

        // 创建复选框列表
        String[] items = {"Java", "Python", "C++", "JavaScript", "Go", "Rust"};
        CheckBoxList checkBoxList = new CheckBoxList(items);
        checkBoxList.setCheckBoxListSelectedIndices(new int[]{0, 2});

        // 添加滚动条
        JScrollPane scrollPane = new JScrollPane(checkBoxList);

        // 创建结果标签
        JLabel resultLabel = new JLabel("已选择: Java, C++");

        // 添加监听器
        CheckBoxListSelectionModel selectionModel =
            (CheckBoxListSelectionModel) checkBoxList.getSelectionModel();
        selectionModel.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                Object[] selected = checkBoxList.getCheckBoxListSelectedValues();
                resultLabel.setText("已选择: " + Arrays.toString(selected));
            }
        });

        frame.add(scrollPane, BorderLayout.CENTER);
        frame.add(resultLabel, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
}
